using UnityEngine;
using UnityEngine.EventSystems;

public class LuaClickListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
{
    public delegate void VoidDelegate(GameObject target, float x, float y);

    public VoidDelegate onClick;

    public VoidDelegate onClickDown;

    public VoidDelegate onClickUp;

    public void OnPointerClick(PointerEventData eventData)
    {
        onClick?.Invoke(eventData.pointerClick, eventData.position.x, eventData.position.y);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        onClickDown?.Invoke(eventData.pointerClick, eventData.position.x, eventData.position.y);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        onClickUp?.Invoke(eventData.pointerClick, eventData.position.x, eventData.position.y);
    }

    public static LuaClickListener Get(GameObject target)
    {
        if (!target.TryGetComponent(out LuaClickListener listener))
        {
            listener = target.AddComponent<LuaClickListener>();
        }
        return listener;
    }
}